package com.xiaoge.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoge.auth.security.UserInfo;
import com.xiaoge.auth.annotation.ControllerEndpoint;
import com.xiaoge.response.Result;
import com.xiaoge.system.entity.Notice;
import com.xiaoge.system.service.NoticeService;
import com.xiaoge.system.vo.NoticeVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 通知公告表 前端控制器
 * </p>
 *
 * @author xiaoge
 * @since 2021-03-19
 */
@RestController
@CrossOrigin
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    /**
     * 分页查询通知
     *
     * @param current
     * @param size
     * @param noticeVo
     * @return
     */
    @ApiOperation(value = "分页查询通知")
    @PostMapping("/findNoticePage")
    public Result findUserPage(@RequestParam(defaultValue = "1") Integer current,
                               @RequestParam(defaultValue = "6") Integer size,
                               @RequestBody NoticeVo noticeVo) {
        //取得分页条件
        Page<Notice> page = new Page<>(current, size);
        //获得wragger
        QueryWrapper<Notice> queryWrapper = getWrapper(noticeVo);

        //执行查询
        IPage<Notice> noticePage = noticeService.page(page, queryWrapper);

        long total = noticePage.getTotal();
        List<Notice> records = noticePage.getRecords();
        return Result.ok().data("total", total).data("records", records);
    }

    /**
     * 查询通知详细信息
     *
     * @param noticeId
     * @return
     */
    @ApiOperation(value = "查询通知详情")
    @GetMapping("/getNoticeById")
    public Result getNotice(Long noticeId) {
        return noticeService.getNoticeById(noticeId);
    }

    /**
     * 添加通知
     *
     * @param notice
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加通知失败", operation = "添加通知")
    @ApiOperation(value = "添加通知")
    @PostMapping("/addNotice")
    public Result addNotice(@RequestBody Notice notice) {
        //获取security当前用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (!(authentication instanceof AnonymousAuthenticationToken)) {
            UserInfo userInfo =(UserInfo) authentication.getPrincipal();
            notice.setCreateBy(userInfo.getId()+userInfo.getUsername());
        }
        noticeService.save(notice);
        return Result.ok();
    }

    /**
     * 修改通知
     *
     * @param notice
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "修改通知失败", operation = "修改通知")
    @ApiOperation(value = "修改通知")
    @PostMapping("/updateNotice")
    public Result updateNotice(@RequestBody Notice notice) {
        //获取security当前用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (!(authentication instanceof AnonymousAuthenticationToken)) {
            UserInfo userInfo =(UserInfo) authentication.getPrincipal();
            notice.setUpdateBy(userInfo.getId()+userInfo.getUsername());
        }
        noticeService.updateById(notice);
        return Result.ok();
    }

    /**
     * 删除通知
     *
     * @param ids
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除通知失败", operation = "删除通知")
    @ApiOperation(value = "删除通知")
    @DeleteMapping("/deleteNotice")
    public Result deleteNotice( String ids) {
        String[] idList = ids.split(",");
        noticeService.removeByIds(Arrays.asList(idList));
        return Result.ok();
    }


    private QueryWrapper<Notice> getWrapper(NoticeVo noticeVo) {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        if (noticeVo != null) {

            //查询每个条件
            if (!ObjectUtils.isEmpty(noticeVo.getNoticeTitle())) {
                queryWrapper.like("notice_title", noticeVo.getNoticeTitle());
            }
            if (!ObjectUtils.isEmpty(noticeVo.getCreateBy())) {
                queryWrapper.like("create_by", noticeVo.getCreateBy());
            }
            if (!ObjectUtils.isEmpty(noticeVo.getUpdateBy())) {
                queryWrapper.like("update_by", noticeVo.getUpdateBy());
            }
            if (!ObjectUtils.isEmpty(noticeVo.getType())) {
                queryWrapper.like("notice_type", noticeVo.getType());
            }
        }
        return queryWrapper;
    }
}
